package class5;

import java.io.File;

import java.io.File;

public class demo4 {
    public static void main(String[] args) {
        String path = "D:" + File.separator + "aa";
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                delete(file);
            } else if (file.isFile()) {
                boolean success = file.delete();
                if (!success) {
                    System.err.println("文件删除失败: " + file.getAbsolutePath());
                }
            }
        } else {
            System.err.println("目标路径不存在: " + path);
        }
    }

    static void delete(File file) {
        File[] list = file.listFiles();
        if (list == null) {
            System.err.println("无法访问目录: " + file.getAbsolutePath());
            return;
        }

        for (File f : list) {
            if (f.isDirectory()) {
                delete(f); // 递归删除子目录
            } else {
                boolean success = f.delete();
                if (!success) {
                    System.err.println("文件删除失败: " + f.getAbsolutePath());
                }
            }
        }

        // 删除当前目录
        boolean dirDeleted = file.delete();
        if (!dirDeleted) {
            System.err.println("目录删除失败: " + file.getAbsolutePath());
        }
    }
}
